package com.example.widgetapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class MangheAdapter extends ArrayAdapter<Manghe> {
    private final int resourseId;

    /*选择构造函数时注意参数的正确选用（倒数第二个）*/
    public MangheAdapter(@NonNull Context context, int resource, @NonNull List<Manghe> objects) {
        super(context, resource, objects);
        this.resourseId = resource;
    }

    /*通过Generate生成Override Methods*/
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Manghe manghe = getItem(position); /*获取当前项的Manghe实例*/

        /*为子项加载传入的布局*/
        View view = LayoutInflater.from(getContext()).inflate(resourseId,parent,false);

        /*调用View的findViewById()获取ImageView和TextView实例*/
        ImageView mangheImage = view.findViewById(R.id.manghe_image);
        TextView mangheText = view.findViewById(R.id.manghe_name);

        /*调用分别ImageView的setImageResourse()方法和TextView的setText()方法来设置显示的图片和文字*/
        mangheImage.setImageResource(manghe.getImageId());
        mangheText.setText(manghe.getName());
        return view;
    }
}
